
/* 
   The class MosaicFram represents a window made up of a grid
   of colored rectangles.  Routines are provided for setting 
   and testing the color of rectangles in the grid.

   Each rectangle in the grid has a color.  The color can be
   specified by red, green, and blue amounts in the range from
   0 to 255.  (It can also be given as an object belonging
   to the class Color.)

   David Eck (eck@hws.edu), 17 January 1998.
   Modified July 24, 1998 to use Java 1.1 style event handling.
*/


import java.awt.*;
import java.awt.event.*;

public class MosaicFrame extends Frame {

   protected MosaicCanvas canvas;  // A component that actually manages and displays the rectangles
   private boolean closed = false; // This will be set to true when the window is closed.

   public MosaicFrame(int rows, int columns, int blockWidth, int blockHeight, String name) {
         // Constuctor creates a window with the specified number of rows and
         // columns of squares.  blockWidth and blockHeight specify the
         // desired width and height of rectangles in the grid.
	   setTitle(name);
      canvas = new MosaicCanvas(rows,columns,blockWidth,blockHeight, name);
      add("Center",canvas);
      addWindowListener(
            new WindowAdapter() {  // close the window when the user clicks its close box
               public void windowClosing(WindowEvent evt) {
                  closed = true;
                  dispose();
               }
            });
      pack();
      show();
   }

   public MosaicFrame() {
         // Constructor creates a 20-by-20 grid of 15-by-15 rectangles
      this(20,20,15,15,null);
   }

   public MosaicFrame(int rows, int columns) {
         // Constructor creates a window with the specified number of rows
         // and columns.  The size of the rectangles is 10-by-10.
      this(rows,columns,15,15,null);
   }
   
   public void repaint()
   {
	   this.canvas.repaint();
   }

   public void delay(int milliseconds) {
         // Calling this routine causes a delay of the specified number
         // of milliseconds in the program that calls the routine.  It is
         // provided here as a convenience.
      if (milliseconds > 0) {
        try { Thread.sleep(milliseconds); }
        catch (InterruptedException e) { }
      }
   }

   public Color getColor(int row, int col) {
         // Returns the object of type Color that represents the color
         // of the grid in the specified row and column.
      return canvas.getColor(row, col);
   }

   public int getRed(int row, int col) {
         // Returns the red component, in the range 0 to 255, of the
         // rectangle in the specified row and column.
      return canvas.getRed(row, col);
   }

   public int getGreen(int row, int col) {
         // Returns the green component, in the range 0 to 255, of the
         // rectangle in the specified row and column.
      return canvas.getGreen(row, col);
   }

   public int getBlue(int row, int col) {
         // Returns the blue component, in the range 0 to 255, of the
         // rectangle in the specified row and column.
      return canvas.getBlue(row, col);
   }

   public void setColor(int row, int col, Color c) {
          // Set the rectangle in the specified row and column to have
          // the specified color.
      canvas.setColor(row,col,c);
   }

   public void setColor(int row, int col, int red, int green, int blue) {
          // Set the rectangle in the specified row and column to have
          // the color with the specifed red, green, and blue components.
      canvas.setColor(row,col,red,green,blue);
   }

   public void fill(Color c) {
          // Set all the rectangels in the grid to the color c.
      canvas.fill(c);
   }

   public void fill(int red, int green, int blue) {
          // Set all the rectangles in the grid to the color with
          // the specified red, green, and blue components.
      canvas.fill(red,green,blue);
   }

   public void fillRandomly() {
          // Sets each rectangle in the grid to a different randomly
          // chosen color.
      canvas.fillRandomly();
   }

   public boolean stillOpen() {
          // This method returns a boolean value that can be used to
          // test whether the window is still open.
      return (!closed);
   }
   
   
}  // end of class MosaicFrame